package com.example.its2021606134chh.TrafficViolationModule;

import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.its2021606134chh.R;

import java.util.ArrayList;
import java.util.List;

public class TVSearchResultsActivity extends AppCompatActivity {

    private ImageView imgTVSearchBack;
    private RecyclerView rvCarResults;
    private List<CarPeccancys> list = new ArrayList<>();
    private ImageView imgItemLeft;
    private ImageView imgItemRight;
    private int index = 0;//控制跳转页的标识
    TVSearchRVAdapter tvSearchRVAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t_v_search_results);

        initView();

        Intent intent = getIntent();
        String sCarNum = intent.getStringExtra("carnumber");
        Log.i("TAG", sCarNum);

        CarSQLiteOpenHelper openHelper;
        SQLiteDatabase database;
        openHelper = new CarSQLiteOpenHelper(TVSearchResultsActivity.this, "CarPeccancy.db", null, 1);
        database = openHelper.getReadableDatabase();
        try (Cursor cursor = database.query("carpeccancy", null, null, null, null, null, null)) {
            if (cursor.moveToFirst()) {
                do {
                    int id = cursor.getInt(0);
                    String carnumber = cursor.getString(1);
                    String pdatetime = cursor.getString(2);
                    String paddr = cursor.getString(3);
                    if (carnumber.equals(sCarNum)) {
                        Log.i("TAG", "id: " + id + ", carnumber: " + carnumber + ", pdatetime: " + pdatetime + ", paddr: " + paddr);
                        list.add(new CarPeccancys(paddr, pdatetime, carnumber));
                    }
                } while (cursor.moveToNext());
            }
        } catch (SQLException ignored) {
            Toast.makeText(TVSearchResultsActivity.this, "没有查询到" + sCarNum + "车的违章数据！", Toast.LENGTH_SHORT).show();
        } finally {
            database.close();
        }

        CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //滑动开关
        linearLayoutManager.setScrollEnabled(false);
        rvCarResults.setLayoutManager(linearLayoutManager);
        tvSearchRVAdapter = new TVSearchRVAdapter(list, TVSearchResultsActivity.this);
        rvCarResults.setAdapter(tvSearchRVAdapter);
        //      添加动画
        rvCarResults.setItemAnimator(new DefaultItemAnimator());
        myOnClick();

        imgTVSearchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void myOnClick() {
        imgItemRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index < list.size() - 1) {
                    index++;
                    Log.i("TAG", "indext++=" + index);
                    tvSearchRVAdapter.addData(index);
                    rvCarResults.scrollToPosition(index);
                    imgItemRight.setVisibility(View.VISIBLE);
                    imgItemLeft.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(TVSearchResultsActivity.this, "已经到底了", Toast.LENGTH_SHORT).show();
                }
            }
        });

        imgItemLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index > 0) {
                    index--;
                    Log.i("TAG", "indext--=" + index);
                    tvSearchRVAdapter.removeData(index);
                    rvCarResults.scrollToPosition(index);
                    imgItemRight.setVisibility(View.VISIBLE);
                    imgItemLeft.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(TVSearchResultsActivity.this, "已经到底了", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        imgTVSearchBack = (ImageView) findViewById(R.id.imgTVSearchBack);
        rvCarResults = (RecyclerView) findViewById(R.id.rv_car_results);
        imgItemLeft = (ImageView) findViewById(R.id.imgItemLeft);
        imgItemRight = (ImageView) findViewById(R.id.imgItemRight);
    }
}